package com.example.tbag.healthcare.Activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;

import com.example.tbag.healthcare.Model.HumanInfo;
import com.example.tbag.healthcare.R;
import com.example.tbag.healthcare.Util.CallPhoneUtil;
import com.example.tbag.healthcare.Util.DatabaseHelper;
import com.example.tbag.healthcare.Util.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by tbag on 2017/4/23.
 */

public class AddHumanInfoActivity extends AppCompatActivity {
    private int id;
    private boolean isShow = false;
    @BindViews({R.id.edit1, R.id.edit2, R.id.edit3, R.id.edit4, R.id.edit5, R.id.edit6,R.id.edit7,R.id.edit8})
    List<EditText> editTexts;
    @BindView(R.id.text)
    TextView textView;
    private DatabaseHelper helper;

    @OnClick(R.id.btn_back)
    public void back() {
        this.finish();
    }

    @OnClick(R.id.btn1)
    public void exit() {
        this.finish();
    }

    @OnClick(R.id.right_btn)
    public void callForHelp() {
        CallPhoneUtil.call(this);
    }

    @OnClick(R.id.btn2)
    public void checkAndSave() {
        int index = 0;
        ContentValues cv = new ContentValues();
        for (EditText editText : editTexts) {
            String text = editText.getText().toString().trim();
            switch (index) {
                case 0:
                    if (text.isEmpty()) {
                        ToastUtil.show(this, "日期不能为空");
                        return;
                    }
                    cv.put("date", text);
                    break;
                case 1:
                    if (text.isEmpty()) {
                        ToastUtil.show(this, "时间不能为空");
                        return;
                    }
                    cv.put("time", text);
                    break;
                case 2:
                    if (text.isEmpty()) {
                        text = "";
                    }
                    cv.put("temper", text);
                    break;
                case 3:
                    if (text.isEmpty()) {
                        text="";
                    }
                    cv.put("pulse", text);
                    break;
                case 4:
                    if (text.isEmpty()) {
                        text = "";
                    }
                    cv.put("breath", text);
                    break;
                case 5:
                    if (text.isEmpty()) {
                        text = "";
                    }
                    cv.put("blood", text);
                    break;
                case 6:
                    if (text.isEmpty()) {
                        text = "";
                    }
                    cv.put("sugar", text);
                    break;
                case 7:
                    if (text.isEmpty()) {
                        text = "";
                    }
                    cv.put("eg", text);
                    break;
            }
            index++;
        }
        if(isShow){
            helper.getWritableDatabase().update("human",cv,"_id = ?",new String[]{id+""});
            ToastUtil.show(this, "修改成功");
        }else {
            cv.put("id", id + "");
            helper.getWritableDatabase().insert("human", null, cv);
            ToastUtil.show(this, "保存成功");
        }
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                exit();
            }
        }, 800);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_human_info);
        ButterKnife.bind(this);
        id = getIntent().getIntExtra("id",0);
        if(getIntent().getIntExtra("type",1)==0){
            isShow = true;
            textView.setText("修改体征信息");
            change();
        }
        helper = new DatabaseHelper(this);
    }

    public HumanInfo getPerson() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("human",null,"_id =?",new String[]{id+""},null,null,null);
        if (cursor != null && cursor.moveToFirst()) {
            HumanInfo humanInfo = getHumanInfo(cursor);
            cursor.close();
            db.close();
            return humanInfo;
        }
        ToastUtil.show(this,"系统出现问题");
        finish();
        return null;
    }

    public HumanInfo getHumanInfo(Cursor cursor) {
        int id1 = cursor.getInt(cursor.getColumnIndex("_id"));
        String date = cursor.getString(cursor.getColumnIndex("date"));
        String time = cursor.getString(cursor.getColumnIndex("time"));
        String temper = cursor.getString(cursor.getColumnIndex("temper"));
        String pulse = cursor.getString(cursor.getColumnIndex("pulse"));
        String breath = cursor.getString(cursor.getColumnIndex("breath"));
        String  blood = cursor.getString(cursor.getColumnIndex("blood"));
        String sugar = cursor.getString(cursor.getColumnIndex("sugar"));
        String eg = cursor.getString(cursor.getColumnIndex("eg"));
        HumanInfo humanInfo = new HumanInfo(id1,date, time, temper, pulse, breath, blood,sugar,eg);
        return humanInfo;
    }

    private void change() {
        HumanInfo humanInfo = getPerson();
        editTexts.get(0).setText(humanInfo.date);
        editTexts.get(1).setText(humanInfo.time);
        editTexts.get(2).setText(humanInfo.temper);
        editTexts.get(3).setText(humanInfo.pulse);
        editTexts.get(4).setText(humanInfo.breath);
        editTexts.get(5).setText(humanInfo.blood);
        editTexts.get(6).setText(humanInfo.sugar);
        editTexts.get(7).setText(humanInfo.eg);
    }

}
